import sys
import pymysql
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
import diagnosis_pca as pca
import joblib


if __name__ == "__main__":
    work_path = sys.argv[2]
    args = sys.argv[1]
    list_args = args[args.find("(")+1:args.find(")")].split(", ")
    keys = list()
    for i in range(1,42):
        keys.append('XMEAS(' + str(i) + ')')
    for i in range(1,12):
        keys.append('XMV(' + str(i) + ')')
    values = list()
    for i in range(2,len(list_args)):
        list_data = list_args[i].split("=")
        values.append(list_data[1])
    dict_data = dict(zip(keys, values))
    pd_data = pd.DataFrame(dict_data, index= [0])
    # print(pd_data)
    model_p = np.load(work_path+'model/model_p.npy')
    model_v = np.load(work_path+'model/model_v.npy')
    with open(work_path+'model/value.txt', 'r') as file:
        data = file.readlines()
        t2_limit = np.array(data[0].split(","), dtype=float)[0]
        spe_limit = np.array(data[1].split(","), dtype=float)[0]

    scaler = joblib.load(work_path+'model/scaler.joblib')
    ## 测试集数据标准化
    Xtest_fault = scaler.transform(pd_data)
    # print(Xtest_fault)
    t2, spe = pca.pca_model_online(Xtest_fault, model_p, model_v)
    print(t2)
    print(spe)




